<?php

require_once 'src/model/mServicosComprados.php';

class aServicosComprados extends mServicosComprados {
    /* datefomat ver data alua 06 
     * mysql : date_format(data_cad, '%s') as data_cad
     */
    protected $sqlInsert = "INSERT INTO servicosComprados(preco, quantidade, idservico, idvenda) VALUES ('%s','%s','%s','%s')";
    protected $sqlUpdate = "UPDATE servicosComprados SET preco='%s', quantidade='%s', idservico='%s', idvenda='%s' WHERE idservicosComprados = '%s'";
    protected $sqlSelect = "SELECT * FROM servicosComprados sc WHERE 1=1 %s %s";
    protected $sqlSelectNome = "SELECT sc.*, s.nome as servico, (SELECT FORMAT(sum(sc1.preco), 2) FROM servicosComprados sc1 WHERE sc1.idvenda='%s') as total, Concat('R$ ', Replace (Replace (Replace (FORMAT(sc.preco, 2), '.', '|'), ',', '.'), '|', ',')) as precoNome FROM servicosComprados sc JOIN servico s USING (idservico) WHERE 1=1 %s %s";
    protected $sqlDelete = "DELETE FROM servicosComprados WHERE idservicosComprados = '%s'";

    public function insert() {
        $sql = sprintf($this->sqlInsert, $this->getPreco(), $this->getQuantidade(), 
                $this->getIdservico(), $this->getIdvenda());
        return $this->RunQuery($sql);
    }

    public function update() {
        $sql = sprintf($this->sqlUpdate, $this->getPreco(), $this->getQuantidade(), 
                $this->getIdservico(), $this->getIdvenda(), 
                $this->getIdservicosComprados());
        return $this->RunQuery($sql);
    }

    public function delete() {
        $sql = sprintf($this->sqlDelete, $this->getIdservicosComprados());
        return $this->RunQuery($sql);
    }
    
    public function select($where='', $order='') {
        $sql = sprintf($this->sqlSelect, $where, $order);
        return $this->RunSelect($sql);
    }
    
    public function selectNome($idvenda, $where='', $order='') {
        $sql = sprintf($this->sqlSelectNome, $idvenda ,$where, $order);
        return $this->RunSelect($sql);
    }
    
    public function load() {
        $rs = $this->select(sprintf("and idservicosComprados='%s'", $this->getIdservicosComprados()));
        $this->setObjServicosComprados($rs[0]['idservicosComprados'], $rs[0]['preco'], $rs[0]['quantidade'], $rs[0]['idservico'], $rs[0]['idvenda']);
        return $this;
    }
    
}
